﻿<!--#include file="../inc/common.asp" -->
<!--#include file="apply_templet.asp" -->
<!--#include file="skin_templet.asp" -->
<%
    if not userRightObj.canManage then
        message("您没有设置权限")
    end if
    call OpenDB
    
    dim actionType, template_id, template_name, template_code, template_code2

    sub saveTemplate()
        template_id = clng(request("template_id"))
        sql = "select * from blog_template where  blog_id=" & blogObj.blogId & " and template_id=" & template_id
        rs.open sql, conn, 1, 3
        if rs.eof then
            rs.addNew()
            rs("blog_id") = blogObj.blogId
            rs("user_id") = userObj.userId
            rs("user_name") = userObj.userName
            rs("parent_id") = request("parent_id")
            rs("insert_time") = Now()
        end if
        dim template_name, template_desc
        template_name = filterHTML(request("template_name"))
        template_desc = filterHTML2(request("template_desc"),true)
        if template_desc="" then
            template_desc = template_name
        end if    
        rs("template_name") = template_name
        rs("template_desc") = template_desc
        if request("is_published")="true" then
            rs("is_published")=true
        else
            rs("is_published")=false
        end if
        rs("update_time") = Now()
        rs("template_code") = request("template_code")
        rs("template_code2") = request("template_code2")
        rs.update()
        
        template_id = rs("template_id")
        
        rs.close()
        

    end sub
    
    sub applyTemplate()
        if isEmpty(template_id) then
            template_id = trim(request("template_id"))
        end if    
        
        '重置当前模板
        sql = "update blog_template set is_default=false where blog_id=" & blogObj.blogId
        conn.execute(sql)
        
        if left(template_id, 7) = "/skins/" then
            applySysTemplet(template_id)
            
        else
            template_id = clng(template_id)
            sql = "update blog_template set is_default=true where blog_id=" & blogObj.blogId & " and template_id=" & template_id
            conn.execute(sql)
            
            sql = "select template_code, template_code2 from blog_template  where blog_id=" & blogObj.blogId & " and template_id=" & template_id
            rs.open sql, conn, 1, 1
            template_code = rs("template_code")
            template_code2= rs("template_code2")
            if isNull(template_code2) or isEmpty(template_code2) or trim(template_code2)="" then
                template_code2 = template_code
            end if
            rs.close()
            
            call applyTemplet(template_code, "index.asp", false)
            call applyTemplet(template_code2, "detail.asp", true)
        end if
        
    end sub
    
    sub previewTemplate()
        template_code = request("template_code")
        if isEmpty(template_code) or template_code="" then
            template_id = trim(request("template_id"))
            if template_id="" then
                template_id = trim(request("parent_id"))
            end if
            if left(template_id, 7) = "/skins/" then
                dim skinData
                skinData = getSysTemplet(template_id)
                template_code = skinData(2)
            else
                template_id = clng(template_id)
                sql = "select template_code from blog_template  where  template_id=" & template_id
                rs.open sql, conn, 1, 1
                if not rs.eof then
                    template_code = rs("template_code")
                end if    
                rs.close()
            end if
        end if
        closeDB()
        dim previewFile
        previewFile = applyTemplet("<html><body><script language=javascript>window.focus();</script></body></html> " & vbCrLf & template_code, "preview.asp", false)
        'server.transfer previewFile
        
        response.redirect "../preview.asp?blog_id=" & blogObj.blogId
        response.end
        
    end sub
    
    sub publishTemplate(isPublished)
        if isEmpty(template_id) then
            template_id = clng(request("template_id"))
        end if    
        sql = "update blog_template set is_published=" & isPublished & " where blog_id=" & blogObj.blogId & " and template_id=" & template_id
        conn.execute(sql)
        
    end sub
    
    actionType = trim(request("actionType"))
    if actionType="create_templet" then
        closeDB()
        transfer "select_templet.asp"
        response.end
    elseif actionType="templet_selected" then
        closeDB()
        transfer "edit_templet.asp"
        response.end
    elseif actionType="edit_templet" then
        closeDB()
        transfer "edit_templet.asp"
        response.end
    elseif actionType="save_templet" then
        call saveTemplate()
    elseif actionType="apply_templet" then
        call applyTemplate()
    elseif actionType="save_apply" then
        call saveTemplate()
        call applyTemplate()
    elseif actionType="delete_templet" then
        template_id = clng(request("template_id"))
        sql = "delete from blog_template where  blog_id=" & blogObj.blogId & " and template_id=" & template_id
        conn.execute(sql)
    elseif actionType="preview_templet" then
        call previewTemplate()
    elseif actionType="publish_templet" then
        call publishTemplate(true)
    elseif actionType="private_templet" then
        call publishTemplate(false)
    end if
%>
<HTML><HEAD><TITLE><%=blogObj.blogTitle%>—控制面板—模板</TITLE>

<link rel="stylesheet" href="<%=cssFile%>" type="text/css" />
<style type="text/css">
td{
    font-size:10pt;
}
</style>

<script language=javascript>
    function assertChecked(){
        theform = document.getElementById("form1");
        isChecked = false;
        if (theform.template_id.length) {
            for (i=0;i<theform.template_id.length;i++) {
                if (theform.template_id[i].checked){
                    isChecked = true;
                    break;
                }    
            }
        }else{
            if (theform.template_id.checked){
                isChecked = true;
            }
        }
        if (!isChecked){
            alert("请选择模板");
            return false;
        }    
        return true;
    }
    
    function createTemplet(){
        theform = document.getElementById("form1");
        theform.actionType.value="create_templet";
        theform.submit();
    }
    function editTemplet(){
        theform = document.getElementById("form1");
        theform.actionType.value="edit_templet";
        
        if (!assertChecked()) return;
        
        theform.submit();
    }
    function deleteTemplet(){
        if (!confirm("确定删除该模板吗?")){
            return;
        }
    
        theform = document.getElementById("form1");
        theform.actionType.value="delete_templet";
        
        if (!assertChecked()) return;
        
        theform.submit();
    }    
    function applyTemplet(){
        if (!assertChecked()) return;
        if (!confirm("确定应用该模板吗?")){
            return;
        }

        theform = document.getElementById("form1");
        theform.actionType.value="apply_templet";
        
        theform.submit();
    }
    
    function publishTemplet(){
        if (!confirm("确认共享该模板以供其他用户使用吗?")){
            return;
        }
        if (!assertChecked()) return;

        theform = document.getElementById("form1");
        theform.actionType.value="publish_templet";
        theform.submit();
    }
    
    function privateTemplet(){
        if (!assertChecked()) return;

        theform = document.getElementById("form1");
        theform.actionType.value="private_templet";
        theform.submit();
    }
    function previewTemplet(){
        if (!assertChecked()) return;

        theform = document.getElementById("form1");
        theform.actionType.value="preview_templet";
        
        tmp=theform.target;
        theform.target = "preview";
        theform.submit();
        theform.target = tmp;
    }
    
    
</script>


</head>
<body bgcolor="#c1c1c1" leftmargin="0" topmargin="0" style="font-size:10pt">
<div style="width:840px;height:100%;">

<div style="float:left;width: 160px; padding-top:20px">
<!--#include file="menu.asp" -->
</div>

<div style="background:#ffffff; width:650px; height:100%;float:left;padding:20px 10px 10px 10px">
<form name=form1 id=form1 action=templet.asp method=post>
<input type=hidden name=blog_id value="<%=blogObj.blogId%>">
<input type=hidden name=actionType value="">
<p><i>您可以选择使用系统模板或<a href="#custom_template">自定义模板</a></i></p>
<p><b>系统预设模板</b>：</p>
<P>
    <input type=button name="preview_templet" value=" 预览 " onclick="previewTemplet()">
    <input type=button name="apply_templet" value=" 应用 " onclick="applyTemplet()">
</p>
<%
call writeSysTemplets(False) ' skin_templet.asp
%>
<P>
    <input type=button name="preview_templet" value=" 预览 " onclick="previewTemplet()">
    <input type=button name="apply_templet" value=" 应用 " onclick="applyTemplet()">
</p>
<hr>
<p><a name="custom_template" />
<b>使用自定义模板</b>：</p>

<p>您已经创建的模板：<input type=button name="create_templet" value="新建模板" onclick="createTemplet()"></p>
<table  border=1 cellspacing=0  style="border-collapse: collapse" bordercolor="gray">
  <tr>
    <td>选择</td><td nowrap>模板名</td><td>描述</td><td nowrap>创建日期</td><td>&nbsp;</td><td>&nbsp;</td>
  </tr>
<%
    sql = " select * from blog_template where blog_id=" & blogObj.blogId
    rs.open sql, conn, 1, 1
    while not rs.eof
%>  
  <tr>
    <td><input type=radio  name="template_id" value="<%=rs("template_id")%>" <% if rs("is_default")=true then response.write("checked") end if%> ></td>
    <td nowrap><%=rs("template_name")%>&nbsp;</td>
    <td><%=rs("template_desc")%>&nbsp;</td>
    <td nowrap><%=rs("insert_time")%></td>
    <td><% if rs("is_default")=true then response.write("当前模板") end if%>&nbsp;</td>
    <td><% if rs("is_published")=true then response.write("共享模板") end if%>&nbsp;</td>
  </tr>
<%
        rs.moveNext()
    wend    
%>  
</table>    
<p>
    <input type=button name="create_templet" value="新建模板" onclick="createTemplet()">
<%
    if rs.recordCount>0 then
%>
    <input type=button name="preview_templet" value=" 预览 " onclick="previewTemplet()">
    <input type=button name="apply_templet" value=" 应用 " onclick="applyTemplet()">
    <input type=button name="edit_templet" value=" 编辑 " onclick="editTemplet()">
    <input type=button name="delete_templet" value=" 删除 " onclick="deleteTemplet()">
    <input type=button name="publish_templet" value=" 共享 " onclick="publishTemplet()">
    <input type=button name="private_templet" value="取消共享" onclick="privateTemplet()">
<%
    end if
    rs.close()
%>    
</p>    
</div>
</body>    
</html>    
<!--#include file="../inc/end.asp" -->
